﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace PhysioAssist2
{
    /// <summary>
    /// Summary description for WS
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WS : System.Web.Services.WebService
    {

        [WebMethod]
        public List<Agenda> ListaAgenda(int dia, int mes, int ano, int profissional)
        {

            List<Agenda> lst = new List<Agenda>();


            dsTableAdapters.agendaTableAdapter ta = new dsTableAdapters.agendaTableAdapter();
            ds.agendaDataTable dt = new ds.agendaDataTable();

            ta.Fill(dt);
            dt.DefaultView.RowFilter = "mes = " + mes + " and ano = " + ano + " and dia = " + dia + " and profissional = " + profissional;

            foreach (System.Data.DataRowView dr in dt.DefaultView)
            {

                Agenda age = new Agenda();

                age.compromisso = dr["compromisso"].ToString();
                age.hora = dr["hora"].ToString();
                age.id = int.Parse(dr["id"].ToString());
                age.dia = int.Parse(dr["dia"].ToString());
                age.mes = int.Parse(dr["mes"].ToString());
                age.ano = int.Parse(dr["ano"].ToString());
                age.profissional = int.Parse(dr["profissional"].ToString());

                lst.Add(age);

            }


            return lst;


        }






        [WebMethod]
        public Profissional GetProfissional(int id)
        {

            Profissional prof = new Profissional();

            dsTableAdapters.profissionalTableAdapter ta = new dsTableAdapters.profissionalTableAdapter();
            ds.profissionalDataTable dt = new ds.profissionalDataTable();

            ta.Fill(dt);
            dt.DefaultView.RowFilter = "id = " + id;

            foreach (System.Data.DataRowView dr in dt.DefaultView)
            {

                prof.id = id;
                prof.cargo = dr["cargo"].ToString();
                prof.foto = dr["foto"].ToString();
                prof.nome = dr["nome"].ToString();
                prof.nascimento = DateTime.Parse(dr["nascimento"].ToString());

            }

            return prof;

        }





        [WebMethod]
        public List<Paciente> ListaPacientes()
        {

            List<Paciente> lst = new List<Paciente>();

            dsTableAdapters.pacienteTableAdapter ta = new dsTableAdapters.pacienteTableAdapter();
            ds.pacienteDataTable dt = new ds.pacienteDataTable();

            ta.Fill(dt);
            dt.DefaultView.Sort = "nome asc";

            foreach (System.Data.DataRowView dr in dt.DefaultView)
            {

                Paciente pac = new Paciente();

                pac.id = int.Parse(dr["id"].ToString());
                pac.foto = dr["foto"].ToString();
                pac.internacao = DateTime.Parse(dr["data_internacao"].ToString());
                pac.nascimento = DateTime.Parse(dr["nascimento"].ToString());
                pac.nome = dr["nome"].ToString();

                lst.Add(pac);

            }


            return lst;

        }





        [WebMethod]
        public Paciente GetPaciente(int id)
        {

            Paciente pac = new Paciente();

            dsTableAdapters.pacienteTableAdapter ta = new dsTableAdapters.pacienteTableAdapter();
            ds.pacienteDataTable dt = new ds.pacienteDataTable();

            ta.Fill(dt);
            dt.DefaultView.RowFilter = "id = " + id;

            foreach (System.Data.DataRowView dr in dt.DefaultView)
            {

                pac.id = int.Parse(dr["id"].ToString());
                pac.foto = dr["foto"].ToString();
                pac.internacao = DateTime.Parse(dr["data_internacao"].ToString());
                pac.nascimento = DateTime.Parse(dr["nascimento"].ToString());
                pac.nome = dr["nome"].ToString();

            }


            return pac;

        }





        [WebMethod]
        public List<Anuncio> ListaAnuncios()
        {

            List<Anuncio> lst = new List<Anuncio>();

            dsTableAdapters.anuncioTableAdapter ta = new dsTableAdapters.anuncioTableAdapter();
            ds.anuncioDataTable dt = new ds.anuncioDataTable();

            ta.Fill(dt);
            dt.DefaultView.Sort = "data desc";

            foreach (System.Data.DataRowView dr in dt.DefaultView)
            {

                Anuncio anun = new Anuncio();

                anun.data = DateTime.Parse(dr["data"].ToString());
                anun.id = int.Parse(dr["id"].ToString());
                anun.mensagem = dr["mensagem"].ToString();
                anun.profissional = int.Parse(dr["profissional"].ToString());

                lst.Add(anun);

            }


            return lst;

        }





        [WebMethod]
        public List<Noticia> ListaNoticias()
        {

            List<Noticia> lst = new List<Noticia>();

            dsTableAdapters.noticiaTableAdapter ta = new dsTableAdapters.noticiaTableAdapter();
            ds.noticiaDataTable dt = new ds.noticiaDataTable();

            ta.Fill(dt);
            dt.DefaultView.Sort = "data desc";

            foreach (System.Data.DataRowView dr in dt.DefaultView)
            {

                Noticia noti = new Noticia();

                noti.conteudo = dr["conteudo"].ToString();
                noti.data = DateTime.Parse(dr["data"].ToString());
                noti.fonte = dr["fonte"].ToString();
                noti.id = int.Parse(dr["id"].ToString());
                noti.titulo = dr["titulo"].ToString();
                noti.url = dr["url"].ToString();

                lst.Add(noti);

            }


            return lst;

        }





        [WebMethod]
        public List<Midia> ListaMidiasPaciente(int pessoa)
        {

            List<Midia> lst = new List<Midia>();

            dsTableAdapters.midia_pacienteTableAdapter ta = new dsTableAdapters.midia_pacienteTableAdapter();
            ds.midia_pacienteDataTable dt = new ds.midia_pacienteDataTable();

            ta.Fill(dt);
            dt.DefaultView.RowFilter = "paciente = " + pessoa;
            dt.DefaultView.Sort = "id desc";

            foreach (System.Data.DataRowView dr in dt.DefaultView)
            {

                Midia midia = new Midia();

                midia = GetMidia(int.Parse(dr["id"].ToString()));

                lst.Add(midia);

            }


            return lst;

        }





        [WebMethod]
        public List<Midia> ListaMidiasProfissional(int pessoa)
        {

            List<Midia> lst = new List<Midia>();

            dsTableAdapters.midia_profissionalTableAdapter ta = new dsTableAdapters.midia_profissionalTableAdapter();
            ds.midia_profissionalDataTable dt = new ds.midia_profissionalDataTable();

            ta.Fill(dt);
            dt.DefaultView.RowFilter = "profissional = " + pessoa;
            dt.DefaultView.Sort = "id desc";

            foreach (System.Data.DataRowView dr in dt.DefaultView)
            {

                Midia midia = new Midia();

                midia = GetMidia(int.Parse(dr["id"].ToString()));

                lst.Add(midia);

            }


            return lst;

        }





        [WebMethod]
        public Midia GetMidia(int id)
        {

            Midia midia = new Midia();

            dsTableAdapters.midiaTableAdapter ta = new dsTableAdapters.midiaTableAdapter();
            ds.midiaDataTable dt = new ds.midiaDataTable();

            ta.Fill(dt);
            dt.DefaultView.RowFilter = "id = " + id;
            dt.DefaultView.Sort = "data desc";

            foreach (System.Data.DataRowView dr in dt.DefaultView)
            {

                midia.data = DateTime.Parse(dr["data"].ToString());
                midia.id = id;
                midia.tipo = int.Parse(dr["tipo"].ToString());
                midia.objeto = dr["objeto"].ToString();
                midia.observacao = dr["observacao"].ToString();

            }


            return midia;

        }







        [WebMethod]
        public List<Profissional> ListaProfissionais()
        {

            List<Profissional> lst = new List<Profissional>();

            dsTableAdapters.profissionalTableAdapter ta = new dsTableAdapters.profissionalTableAdapter();
            ds.profissionalDataTable dt = new ds.profissionalDataTable();

            ta.Fill(dt);
            dt.DefaultView.Sort = "nome asc";

            foreach (System.Data.DataRowView dr in dt.DefaultView)
            {

                Profissional pro = new Profissional();

                pro.cargo = dr["cargo"].ToString();
                pro.foto = dr["foto"].ToString();
                pro.id = int.Parse(dr["id"].ToString());
                pro.nome = dr["nome"].ToString();
                pro.nascimento = DateTime.Parse(dr["nascimento"].ToString());

                lst.Add(pro);

            }


            return lst;

        }








        [WebMethod]
        public Anuncio GetAnuncio(int id)
        {

            Anuncio aviso = new Anuncio();

            dsTableAdapters.anuncioTableAdapter ta = new dsTableAdapters.anuncioTableAdapter();
            ds.anuncioDataTable dt = new ds.anuncioDataTable();

            ta.Fill(dt);
            dt.DefaultView.RowFilter = "id = " + id;

            foreach (System.Data.DataRowView dr in dt.DefaultView)
            {
                aviso.data = DateTime.Parse(dr["data"].ToString());
                aviso.id = id;
                aviso.mensagem = dr["mensagem"].ToString();
                aviso.profissional = int.Parse(dr["profissional"].ToString());
            }

            return aviso;

        }








        [WebMethod]
        public List<Mensagem> ListaMensagens(int profissionalpara)
        {

            List<Mensagem> lst = new List<Mensagem>();

            dsTableAdapters.mensagemTableAdapter ta = new dsTableAdapters.mensagemTableAdapter();
            ds.mensagemDataTable dt = new ds.mensagemDataTable();

            ta.Fill(dt);
            dt.DefaultView.RowFilter = "profissional_para = " + profissionalpara;
            dt.DefaultView.Sort = "data desc";

            foreach (System.Data.DataRowView dr in dt.DefaultView)
            {

                Mensagem msg = new Mensagem();

                msg.data = DateTime.Parse(dr["data"].ToString());
                msg.id = int.Parse(dr["id"].ToString());
                msg.mensagem = dr["mensagem"].ToString();
                msg.profissional_de = int.Parse(dr["profissional_de"].ToString());
                msg.profissional_para = profissionalpara;

                lst.Add(msg);

            }


            return lst;

        }






        [WebMethod]
        public List<Mensagem> ListaMensagensEnviadas(int profissionalde)
        {

            List<Mensagem> lst = new List<Mensagem>();

            dsTableAdapters.mensagemTableAdapter ta = new dsTableAdapters.mensagemTableAdapter();
            ds.mensagemDataTable dt = new ds.mensagemDataTable();

            ta.Fill(dt);
            dt.DefaultView.RowFilter = "profissional_de = " + profissionalde;
            dt.DefaultView.Sort = "data desc";

            foreach (System.Data.DataRowView dr in dt.DefaultView)
            {

                Mensagem msg = new Mensagem();

                msg.data = DateTime.Parse(dr["data"].ToString());
                msg.id = int.Parse(dr["id"].ToString());
                msg.mensagem = dr["mensagem"].ToString();
                msg.profissional_de = int.Parse(dr["profissional_de"].ToString());
                msg.profissional_para = int.Parse(dr["profissional_para"].ToString());

                lst.Add(msg);

            }


            return lst;

        }








        [WebMethod]
        public Mensagem GetMensagem(int id)
        {

            Mensagem msg = new Mensagem();

            dsTableAdapters.mensagemTableAdapter ta = new dsTableAdapters.mensagemTableAdapter();
            ds.mensagemDataTable dt = new ds.mensagemDataTable();

            ta.Fill(dt);
            dt.DefaultView.RowFilter = "id = " + id;

            foreach (System.Data.DataRowView dr in dt.DefaultView)
            {
                msg.data = DateTime.Parse(dr["data"].ToString());
                msg.id = int.Parse(dr["id"].ToString());
                msg.mensagem = dr["mensagem"].ToString();
                msg.profissional_de = int.Parse(dr["profissional_de"].ToString());
                msg.profissional_para = int.Parse(dr["profissional_para"].ToString());

            }

            return msg;

        }







        [WebMethod]
        public List<Profissional> ListaAniversariosProfissional(int mes)
        {

            List<Profissional> lst = new List<Profissional>();

            dsTableAdapters.profissionalTableAdapter ta = new dsTableAdapters.profissionalTableAdapter();
            ds.profissionalDataTable dt = new ds.profissionalDataTable();

            ta.Fill(dt);
            dt.DefaultView.Sort = "nascimento asc";

            foreach (System.Data.DataRowView dr in dt.DefaultView)
            {

                Profissional pro = new Profissional();

                pro.cargo = dr["cargo"].ToString();
                pro.foto = dr["foto"].ToString();
                pro.id = int.Parse(dr["id"].ToString());
                pro.nome = dr["nome"].ToString();
                pro.nascimento = DateTime.Parse(dr["nascimento"].ToString());

                if (pro.nascimento.Month == mes) lst.Add(pro);

            }


            return lst;

        }





        [WebMethod]
        public List<Paciente> ListaAniversariosPaciente(int mes)
        {

            List<Paciente> lst = new List<Paciente>();

            dsTableAdapters.pacienteTableAdapter ta = new dsTableAdapters.pacienteTableAdapter();
            ds.pacienteDataTable dt = new ds.pacienteDataTable();

            ta.Fill(dt);
            //dt.DefaultView.Sort = "Day(nascimento)";

            foreach (System.Data.DataRowView dr in dt.DefaultView)
            {

                Paciente pac = new Paciente();

                pac.id = int.Parse(dr["id"].ToString());
                pac.foto = dr["foto"].ToString();
                pac.internacao = DateTime.Parse(dr["data_internacao"].ToString());
                pac.nascimento = DateTime.Parse(dr["nascimento"].ToString());
                pac.nome = dr["nome"].ToString();

                if (pac.nascimento.Month == mes) lst.Add(pac);

            }


            return lst;

        }








        public static int UltimaMidia()
        {

            int ultima = 0;

            dsTableAdapters.midiaTableAdapter ta = new dsTableAdapters.midiaTableAdapter();
            ds.midiaDataTable dt = new ds.midiaDataTable();

            ta.Fill(dt);

            foreach (System.Data.DataRowView dr in dt.DefaultView)
            {

                ultima = int.Parse(dr["id"].ToString());

            }



            return ultima;

        }


    }
}
